package com.finddeer.bookkeeping;

import android.support.annotation.NonNull;

import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

/**
 * author    : leaves
 * gitee     : https://gitee.com/LEAVES2018
 * time      : 2018/12/23 10:04 PM
 * function  : "账单"模型类，定义了账单的基本属性；实现了Comparable接口，以日期为比较凭据，返回天数差
 */
public class Bill implements Comparable {

    private UUID mId;   //  ID
    private String mTitle;  //  标题（详细信息）
    private double mAmount; //  金额
    private String mTag;    //  标签（分类用）
    private Date mDate;     //  日期
    private String mLocale; //  消费地点

    public Bill() {
        this(UUID.randomUUID());
    }

    public Bill(UUID id) {
        mId = id;
        mDate = new Date();
    }

    public Bill(String title, double amount, Date date, String locale) {
        mTitle = title;
        mAmount = amount;
        mDate = date;
        mLocale = locale;
    }

    public UUID getId() {
        return mId;
    }

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        mTitle = title;
    }

    public double getAmount() {
        return mAmount;
    }

    public void setAmount(double amount) {
        mAmount = amount;
    }

    public String getTag() {
        return mTag;
    }

    public void setTag(String tag) {
        mTag = tag;
    }

    public Date getDate() {
        return mDate;
    }

    public void setDate(Date date) {
        mDate = date;
    }

    public String getLocale() {
        return mLocale;
    }

    public void setLocale(String locale) {
        mLocale = locale;
    }

    @Override
    public int compareTo(@NonNull Object o) {
        Bill bill = (Bill) o;

        Calendar fromCalendar = Calendar.getInstance();
        //  Calendar为抽象类，不能使用new，而是使用getInstance()方法获取实例
        fromCalendar.setTime(bill.getDate());
        fromCalendar.set(Calendar.HOUR_OF_DAY, fromCalendar.getMinimum(Calendar.HOUR_OF_DAY));
        fromCalendar.set(Calendar.MINUTE, fromCalendar.getMinimum(Calendar.MINUTE));
        fromCalendar.set(Calendar.SECOND, fromCalendar.getMinimum(Calendar.SECOND));
        fromCalendar.set(Calendar.MILLISECOND, fromCalendar.getMinimum(Calendar.MILLISECOND));
        //  将时、分、秒置零，避免出现时间差小于24小时时不算一天的情况
        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setTime(getDate());
        toCalendar.set(Calendar.HOUR_OF_DAY, toCalendar.getMinimum(Calendar.HOUR_OF_DAY));
        toCalendar.set(Calendar.MINUTE, toCalendar.getMinimum(Calendar.MINUTE));
        toCalendar.set(Calendar.SECOND, toCalendar.getMinimum(Calendar.SECOND));
        toCalendar.set(Calendar.MILLISECOND, toCalendar.getMinimum(Calendar.MILLISECOND));

        long dayDistance = (toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000*3600*24);
        //  toCalendar对应的ms值减去fromCalendar对应的ms值，而后除以一天的时间
        return (int) dayDistance;
    }   //  以日期为比较凭据，返回天数差值
}
